package com.douyang.service.impl;


import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.dao.SaTokenDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douyang.base.enumeration.RoleEnum;
import com.douyang.base.model.UserBaseInfo;
import com.douyang.base.utils.UserUtil;
import com.douyang.mapper.UserRoleMapper;
import com.douyang.pojo.UserRole;
import com.douyang.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2025-03-03
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<String> getUserRole(Long userId) {
        return userRoleMapper.getUserRole(userId);
    }

    @Override
    public void signAgreePresenter() {
        UserBaseInfo loginUserBaseInfo = UserUtil.getLoginUserBaseInfo();
        boolean exists = lambdaQuery()
                .eq(UserRole::getUserId, loginUserBaseInfo.getId())
                .eq(UserRole::getRoleId, RoleEnum.PRESENTER).exists();

        if(exists)return;
        UserRole userRole = new UserRole();
        userRole.setUserId(loginUserBaseInfo.getId());
        userRole.setRoleId(RoleEnum.PRESENTER);
        save(userRole);

        SaTokenDao dao = SaManager.getSaTokenDao();
        dao.deleteObject("user_role:" + loginUserBaseInfo.getId().toString());

    }
}
